package com.site.wisdom_construction.controller.userAuthorityModule;

import com.site.wisdom_construction.entity.userAuthorityModule.Role;
import com.site.wisdom_construction.responseEntity.DataResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.userAuthorityModule.ProjectRoleService;
import com.site.wisdom_construction.service.userAuthorityModule.RoleService;
import com.site.wisdom_construction.service.userAuthorityModule.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/** @Author daiyifei @Date 2021/12/28 @Description */
@Api(tags = "模块接口")
@RestController
@RequestMapping("Role")
@CrossOrigin
public class RoleController {
  @Autowired private RoleService roleService;

  @Autowired private ProjectRoleService projectRoleService;

  @Autowired private UserRoleService userRoleService;

  @ApiOperation(value = "分页模糊查询模块接口", notes = "测试数据:")
  @PostMapping("/findRoleByPage")
  public Page findRoleByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(roleService.Total(map));
    page.setItems(roleService.findRoleByPage(map));
    return page;
  }

  @ApiOperation(value = "项目对应的功能模块", notes = "测试数据:")
  @PostMapping("/findProjectRole")
  public DataResult findProjectRole(@RequestBody Map map) {
    DataResult dataResult = new DataResult();
    /** 查询基础模块*/
    map.put("roleCategory", 1);
    //根据项目id查询对应功能模块
    List<Integer> integerBasicsList = projectRoleService.findProjectRoleByProjectId(map);
    //功能模块List(已开通的功能模块)
    List<Role> roleBasicsList = new ArrayList<Role>();
    for (int i = 0;i<integerBasicsList.size();i++){
      int id = integerBasicsList.get(i);
      roleBasicsList.add(roleService.findRoleById(id));
    }
    //查询所有基础模块信息
    List<Integer> integerBasicsLists = roleService.findRoleByAll(map);
    //取项目对应的模块id数组与所有模块信息数组的差值(id)
    List<Integer> integerBasicsListW = integerBasicsLists.stream().filter(item -> !integerBasicsList.contains(item)).collect(toList());
    //未开通的功能模块
    List<Role> roleBasicsListW = new ArrayList<Role>();
    for (int i = 0;i<integerBasicsListW.size();i++){
      int id = integerBasicsListW.get(i);
      roleBasicsListW.add(roleService.findRoleById(id));
    }
    //封装到返回实体类中
    //已开通的功能模块
    dataResult.setOpenedBasicsData(roleBasicsList);
    //未开通的功能模块
    dataResult.setNotOpenedBasicsData(roleBasicsListW);

    /** 查询物联设备模块*/
    map.put("roleCategory", 2);
    //根据项目id查询对应功能模块
    List<Integer> integerEquipmentList = projectRoleService.findProjectRoleByProjectId(map);
    //功能模块List(已开通的功能模块)
    List<Role> roleEquipmentList = new ArrayList<Role>();
    for (int i = 0;i<integerEquipmentList.size();i++){
      int id = integerEquipmentList.get(i);
      roleEquipmentList.add(roleService.findRoleById(id));
    }
    //查询所有基础模块信息
    List<Integer> integerEquipmentLists = roleService.findRoleByAll(map);
    //取项目对应的模块id数组与所有模块信息数组的差值(id)
    List<Integer> integerEquipmentListW = integerEquipmentLists.stream().filter(item -> !integerEquipmentList.contains(item)).collect(toList());
    //未开通的功能模块
    List<Role> roleEquipmentListW = new ArrayList<Role>();
    for (int i = 0;i<integerEquipmentListW.size();i++){
      int id = integerEquipmentListW.get(i);
      roleEquipmentListW.add(roleService.findRoleById(id));
    }
    //封装到返回实体类中
    //已开通的功能模块
    dataResult.setOpenedEquipmentData(roleEquipmentList);
    //未开通的功能模块
    dataResult.setNotOpenedEquipmentData(roleEquipmentListW);

    /** 查询5G互联模块*/
    map.put("roleCategory", 3);
    //根据项目id查询对应功能模块
    List<Integer> integerConnectList = projectRoleService.findProjectRoleByProjectId(map);
    //功能模块List(已开通的功能模块)
    List<Role> roleConnectList = new ArrayList<Role>();
    for (int i = 0;i<integerConnectList.size();i++){
      int id = integerConnectList.get(i);
      roleConnectList.add(roleService.findRoleById(id));
    }
    //查询所有基础模块信息
    List<Integer> integerConnectLists = roleService.findRoleByAll(map);
    //取项目对应的模块id数组与所有模块信息数组的差值(id)
    List<Integer> integerConnectListW = integerConnectLists.stream().filter(item -> !integerConnectList.contains(item)).collect(toList());
    //未开通的功能模块
    List<Role> roleConnectListW = new ArrayList<Role>();
    for (int i = 0;i<integerConnectListW.size();i++){
      int id = integerConnectListW.get(i);
      roleConnectListW.add(roleService.findRoleById(id));
    }
    //封装到返回实体类中
    //已开通的功能模块
    dataResult.setOpenedConnectData(roleConnectList);
    //未开通的功能模块
    dataResult.setNotOpenedConnectData(roleConnectListW);
    return dataResult;
  }

  @ApiOperation(value = "用户对应的模块使用权限", notes = "测试数据:")
  @PostMapping("/findUserRole")
  public OneDataResult findUserRole(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    //存储已有权限的模块
    List<Role> userRoleList = new ArrayList<Role>();
    //查询对应用户拥有的权限模块
    List<Integer> integerList = userRoleService.findUserRoleByUserId(map);
    for (int i = 0;i<integerList.size();i++){
      int id = integerList.get(i);
      userRoleList.add(roleService.findRoleById(id));
    }
    oneDataResult.setData(userRoleList);
    return oneDataResult;
  }
}
